package com.example.demo1.controller;

import com.example.demo1.model.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-01-10
 * Time: 16:05
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @GetMapping("/getList")
    public List<Message> getList() {
        return messageService.queryMessage();
    }

    @PostMapping(value = "/publish",produces = "application/json")
    public String publish(@RequestBody Message message) {
        if(StringUtils.hasLength(message.getMessage())
        && StringUtils.hasLength(message.getFrom())
        && StringUtils.hasLength(message.getTo())) {
            messageService.insertMessage(message);
            return "{ \"ok\": 1}";
        }
        return "{\"ok\": 0}";
    }
}
